package bb.lanxing.manager;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.model.LatLng;

import java.util.Timer;
import java.util.TimerTask;

import bb.lanxing.App;
import bb.lanxing.calc.data.DisplayPoint;
import bb.lanxing.util.BiCiCoorConverter;
import bb.lanxing.util.CommonUtil;
import bb.lanxing.util.Log;

public class BaiduLocationManager {
    private static final int BAIDU_LOCATION_INTERVAL = 300000;
    private static final String TAG = "BaiduLocationManager";
    private static volatile BaiduLocationManager instance;
    private LocationClient bdLocationClient;
    private BDAbstractLocationListener bdLocationListener = new BDAbstractLocationListener() {
        @Override
        public void onReceiveLocation(BDLocation bDLocation) {
            LatLng common2Earth;
            if (bDLocation == null) {
                return;
            }
            if (App.getContext().isDebugMode()) {
                Log.d(BaiduLocationManager.TAG, "onReceiveLocation: " + bDLocation.getLatitude() + ", " + bDLocation.getLongitude() + ", " + bDLocation.getRadius() + ", " + bDLocation.getCity());
            }
            if (!CommonUtil.isPointValid(bDLocation.getLatitude(), bDLocation.getLongitude()) || bDLocation.getRadius() <= 0.0f) {
                return;
            }
            if (bdLocationClient != null) {
                bdLocationClient.stop();
            }
            if (!SharedManager.getInstance().getLatlngValue().equals("0")) {
                String latlngValue = SharedManager.getInstance().getLatlngValue();
                latlngValue.hashCode();
                if (latlngValue.equals("1")) {
                    common2Earth = new LatLng(bDLocation.getLatitude(), bDLocation.getLongitude());
                } else if (latlngValue.equals("2")) {
                    common2Earth = BiCiCoorConverter.common2Earth(new LatLng(bDLocation.getLatitude(), bDLocation.getLongitude()));
                } else {
                    common2Earth = BiCiCoorConverter.baidu2Earth(new LatLng(bDLocation.getLatitude(), bDLocation.getLongitude()));
                }
            } else {
                common2Earth = BiCiCoorConverter.common2Earth(new LatLng(bDLocation.getLatitude(), bDLocation.getLongitude()));
            }
            LatLng latLng = common2Earth;
            if (latLng == null) {
                return;
            }
            if (listener != null) {
                listener.onLocationChanged(latLng);
            }
            SharedManager.getInstance().setCurLocationInfoWithMP(latLng.latitude, latLng.longitude, bDLocation.getCity(), bDLocation.getProvince());
            DisplayPoint displayPoint = new DisplayPoint();
            displayPoint.setAltitude(bDLocation.getAltitude());
            displayPoint.setSpeed((double) bDLocation.getSpeed());
            App.getContext().uploadMyLocationIfNeed(latLng, displayPoint, null, 0, false, null);
        }
    };
    private LocationChangedListener listener;
    private Timer timer;

    public interface LocationChangedListener {
        void onLocationChanged(LatLng latLng);
    }

    private BaiduLocationManager() {
    }

    public static BaiduLocationManager getInstance() {
        if (instance == null) {
            synchronized (BaiduLocationManager.class) {
                if (instance == null) {
                    instance = new BaiduLocationManager();
                }
            }
        }
        return instance;
    }

    public static void terminate() {
        if (instance != null) {
            instance.release();
            instance = null;
        }
    }

    private void release() {
        bdLocationClient = null;
        bdLocationListener = null;
        timer = null;
    }

    public void openBaiduGps() {
        initBaiduGps();
        int vDelay = App.getContext().isBatterySavingVersion()
                ? SharedManager.getInstance().getBaiduLocationIntervalWithMP() : BAIDU_LOCATION_INTERVAL;
        try {
            timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    if (bdLocationClient == null || bdLocationClient.isStarted()) {
                        return;
                    }
                    bdLocationClient.start();
                }
            }, vDelay, vDelay);
        } catch (Exception e) {
            Log.e(TAG, "openBaiduGps, Timer, e = " + e);
            e.printStackTrace();
        }
    }

    public boolean isOpening() {
        return timer != null;
    }

    public void closeBaiduGps() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        if (bdLocationClient != null) {
            if (bdLocationListener != null) {
                bdLocationClient.unRegisterLocationListener(bdLocationListener);
            }
            bdLocationClient.stop();
            bdLocationClient = null;
        }
    }

    private void initBaiduGps() {
        if (bdLocationClient == null) {
            LocationClient.setAgreePrivacy(true);
            try {
                bdLocationClient = new LocationClient(App.getContext());
            } catch (Exception e) {
                e.printStackTrace();
            }
            bdLocationClient.registerLocationListener(bdLocationListener);
            LocationClientOption locationClientOption = new LocationClientOption();
            locationClientOption.setLocationMode(LocationClientOption.LocationMode.Battery_Saving);
            locationClientOption.setIsNeedAddress(true);
            locationClientOption.setScanSpan(5);
            locationClientOption.setTimeOut(20);
            bdLocationClient.setLocOption(locationClientOption);
            bdLocationClient.start();
        }
    }

    public void fetchLocationOnce() {
        if (bdLocationClient == null) {
            initBaiduGps();
        }
        bdLocationClient.start();
    }

    public void setLocationChangedListener(LocationChangedListener locationChangedListener) {
        listener = locationChangedListener;
    }
}